/**
 * Genarator by KKDGen v<?php echo $version."\n";?>
 * @author KKDeveloeper
 * @email Dixonsatit@gmail.com
 * @link www.ikkdev.com,www.kkdeveloper.com
 <?php  echo empty($nameSpace)?'':'* Namespace : '.$nameSpace."\n"; ?>
 * ExtjsForm : <?php echo $modelClass."\n";?>
 */
 
Ext.define('<?php echo empty ($nameSpace)? $modelClass:$nameSpace.'.view.'.strtolower($modelClass).'.form' ?>', {
    extend: 'Ext.form.Panel',
    alias: 'widget.<?php echo strtolower($modelClass); ?>Form', 
    initComponent: function() {
        
        this.addEvents('<?php echo $modelClass?>');
        Ext.apply(this, {
            activeRecord: null,
            St:null,
            border: false,
            bodyPadding:15,
            items : [<?php 
$count = count($columns);
$i=1;

foreach ($columns as $column): 

if($column->isPrimaryKey===true){ ?> {
            xtype: 'displayfield',
            name: '<?php echo $column->name?>',
            fieldLabel: '<?php echo ucfirst($column->name)?>'
     } <?php }
else {
    
        if(strstr($column->dbType,'integer')){ ?> {
                xtype: 'numberfield',
                name: '<?php echo $column->name?>',
                fieldLabel: '<?php echo ucfirst($column->name)?>',
                minValue: 0,
                maxValue: 100
            } <?php }else if(strstr($column->dbType,'date')){ ?>{
                xtype: 'datefield',
               format: 'Y-m-d',
                name: '<?php echo $column->name?>',
                fieldLabel: '<?php echo ucfirst($column->name)?>'
            }<?php } else if(strstr($column->dbType,'text')){ ?>{
                xtype: 'textareafield',
                name: '<?php echo $column->name?>',
                fieldLabel: '<?php echo ucfirst($column->name)?>',
                allowBlank: false
            } <?php } else { ?> {
                xtype: 'textfield',
                name: '<?php echo $column->name?>',
                fieldLabel: '<?php echo ucfirst($column->name)?>',
                allowBlank: false
            } <?php }
}
    $dot = $count==$i?"\n":",\n";
    echo $dot;
    $i++;
    
endforeach; 
?>],
            dockedItems : [{
                xtype: 'toolbar',
                dock: 'top',
                items: [{
                    iconCls: 'icon-back',
                    text:'Back',
                    action: 'back'
                },'-',{
                    text: 'Save',
                    iconCls: 'icon-save',
                    action: 'save',
                    handler:this.onSave,
                    scope: this,
                    itemId: 'save'
                },{
                    text: 'Delete',
                    iconCls: 'icon-delete',
                    action: 'delete',
                    itemId: 'deleteInform',
                    disabled:true
                },'-',{
                    text: 'Add',
                    iconCls: 'icon-add',
                    action: 'add', 
                    tooltip: 'เพื่มข้อมูล'
                }]
            }],
            bbar: Ext.create('Ext.ux.StatusBar', {
                id: '<?php echo strtolower($modelClass); ?>-statusbar',
                defaultText: 'Ready..',
                text: 'Ready',
                iconCls: 'x-status-valid'
            })
        });

        this.on('fieldvaliditychange',this.updateErrorState,this);
        this.on('fielderrorchange',this.updateErrorState,this);
        this.callParent(arguments);
    }, 
    setActiveRecord: function(record){
        this.activeRecord = record;
        if (record) {
        //this.down('#save').enable();
        //this.getForm().loadRecord(record);
        } else {
        //this.down('#save').disable();
        //this.getForm().reset();
        }
        /*====================*/
        console.log('ActiveRecord : '+record);
    },
    onSave: function()
    {
        var active = this.activeRecord,
        form = this.getForm();
        
        if (form.isValid()) 
        {
            if(active)
            {
                console.log('===update======');
                form.updateRecord(active);
               // this.onReset();
            }
            else
            {
                console.log('===Create======');
                this.fireEvent('create', this, form.getValues());
                this.onReset();
            }
            
            this.setStatusbar('Save complate..');
           
        }

    },
    onReset: function(){
        this.setActiveRecord(null);
        this.getForm().reset();
    },
    loadStatusbar : function()
    { 
        if(this.St==null) this.St=Ext.getCmp('<?php echo strtolower($modelClass); ?>-statusbar');
        else this.St.clearStatus(); 
    },
    setStatusbar : function(msg)
    {
        this.loadStatusbar();
        this.St.setStatus({
            text:msg,
             iconCls: 'x-status-valid',
            clear: true // auto-clear after a set interval
        });
    },
    setStatusbarBusy : function()
    {
        this.loadStatusbar();
        this.St.showBusy();
    },
    updateErrorState: function() {
        var me = this,errorCmp, fields, errors;

        if (me.hasBeenDirty || me.getForm().isDirty()) { //prevents showing global error when form first loads
            errorCmp = me.down('#formErrorState');
            fields = me.getForm().getFields();
            errors = [];
            fields.each(function(field) {
                Ext.Array.forEach(field.getErrors(), function(error) {
                    errors.push({
                        name: field.getFieldLabel(), 
                        error: error
                    });
                });
            });
        // this.down('#save').setDisabled(errors.length!=0);
        }
    }
});


